﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace Byteabyte
{
    class IcsEvent
    {
        public string Uid { get; set; }
        public DateTime DtStart { get; set; }
        public DateTime DtEnd { get; set; }
        public DateTime DtStamp { get; set; }
        public string Summary { get; set; }
        public string Description { get; set; }
        public string Priority { get; set; }

        public List<IcsAlarm> Alarms;

        internal void ParseEvent(System.IO.StreamReader reader)
        {
            string icsLine = string.Empty;
            while ((icsLine = reader.ReadLine()) != IcsStrings.IcsEventEnd)
            {
                if (icsLine == null) break;
                if (icsLine.ToUpper().Equals(IcsStrings.IcsAlarmStart))
                {
                    var icsAlarm = CreateAlarm();
                    icsAlarm.ParseAlarm(reader);
                }
                
                ParseIcsLine(icsLine);
            }
        }

        private IcsAlarm CreateAlarm()
        {
            if (Alarms == null) Alarms = new List<IcsAlarm>();
            var al = new IcsAlarm();
            Alarms.Add(al);
            return al;
        }

        private void ParseIcsLine(string icsLine)
        {
            var lineParts = icsLine.Split(":".ToCharArray());
            if (lineParts.Length < 2) return;
            string lineHeader = lineParts[0].ToUpper();
            string lineValue = string.Empty;
            for (int i = 1; i < lineParts.Length; i++)
            {
                lineValue += lineParts[i];
                lineValue += ":";
            }
            lineValue = lineValue.Substring(0, lineValue.Length - 1);

            switch (lineHeader)
            {
                case IcsStrings.IcsEventUidHeader:
                    {
                        Uid = lineValue;
                        break;
                    }
                case IcsStrings.IcsEventDtStartHeader:
                    {
                        DtStart = DateTime.ParseExact(lineValue, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
                        break;
                    }
                case IcsStrings.IcsEventDtEndHeader:
                    {
                        DtEnd = DateTime.ParseExact(lineValue, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
                        break;
                    }
                case IcsStrings.IcsEventDtStampHeader:
                    {
                        DtStamp = DateTime.ParseExact(lineValue, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
                        break;
                    }
                case IcsStrings.IcsEventSummaryHeader:
                    {
                        Summary = lineValue;
                        break;
                    }
                case IcsStrings.IcsEventDescriptionHeader:
                    {
                        Description = lineValue;
                        break;
                    }
                case IcsStrings.IcsEventPriorityHeader:
                    {
                        Priority = lineValue;
                        break;
                    }
            }
        }
    }
}
